package com.yanq.liancar.activity;
/*
 *  @创建者     liyan
 *  @创建时间   2018/10/23  15:30
 *  @描述       ${TODO}
 */


import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxBarTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.adapter.OrderPagerAdapter;
import com.yanq.liancar.bean.BaseUserInfo;
import com.yanq.liancar.bean.TotalIntegralBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.fragment.IntegralDetailFragment;
import com.yanq.liancar.url.Config;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyPointsActivity extends AppCompatActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_TotalIntegral)
    TextView tvTotalIntegral;
    private List<String> titles;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mypoints);
        ButterKnife.bind(this);
        RxBarTool.setStatusBarColor(this, R.color.jifenbg);
        initViewPager();
        initgetInfo();
    }

    private void initViewPager() {
        // 创建一个集合,装填Fragment
        List<Fragment> fragments = new ArrayList<>();
        // 装填
        fragments.add(IntegralDetailFragment.newInstance(0));
        fragments.add(IntegralDetailFragment.newInstance(1));

        titles = new ArrayList<>();
        titles.add("获得积分明细");
        titles.add("消费积分明细");

        // 创建ViewPager适配器
        OrderPagerAdapter myPagerAdapter = new OrderPagerAdapter(getSupportFragmentManager(), fragments, titles);

        // 给ViewPager设置适配器
        viewPager.setAdapter(myPagerAdapter);
        // TabLayout 指示器 (记得自己手动创建4个Fragment,注意是 app包下的Fragment 还是 V4包下的 Fragment)
        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(0)), true);
        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(1)));

        // 使用 TabLayout 和 ViewPager 相关联
        mTabLayout.setupWithViewPager(viewPager);
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabLayout, 40, 40);
            }
        });
    }

    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    @OnClick({R.id.img_back, R.id.img_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.img_right:
                RxActivityTool.skipActivity(this, IntegralExplanationActivity.class);
                break;
        }
    }

    //
    private void initgetInfo() {
        OkGo.<String>post(Config.s+ "/llc/api/ConsumpeIntegralRecordController/selectTotalPoints")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        TotalIntegralBean info = gson.fromJson(response.body(), TotalIntegralBean.class);
                        if (info.isIsSuccess()) {
                            tvTotalIntegral.setText(info.getData()+"");
                        } else {
                            RxToast.error(info.getOutputMessage());
                        }
                    }
                });
    }
}
